package com.fjnu.dao.impl;

import com.fjnu.dao.ArticleDao;
import com.fjnu.domain.entity.Article;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @desc:
 * @author: Ping
 * @date: 2025/10/17 9:53
 */

@Repository
public class ArticleDaoImpl implements ArticleDao {

    @Autowired
    JdbcTemplate jdbcTemplate ;


    private ArticleRowMapper articleRowMapper = new ArticleRowMapper();




    @Override
    public int insert(Article article) {
        // 检查必填字段是否为空
        if (article.getUserId() == null || article.getTitle() == null ||
                article.getContent() == null || article.getCategoryId() == null) {
            return 0; // 插入失败
        }

        String sql = "INSERT INTO tb_article (user_id, title, content, category_id, create_time, update_time) " +
                "VALUES (?, ?, ?, ?, NOW(), NOW())";

        int result = jdbcTemplate.update(sql,
                article.getUserId(),
                article.getTitle(),
                article.getContent(),
                article.getCategoryId());
        return result > 0 ? 1 : 0;

    }

    @Override
    public int updateStatus(Integer id, Integer newStatus) {
        return 0;
    }

    @Override
    public int updateContent(Integer articleId, String newContent) {

        String sql = "UPDATE tb_article SET content = ?, update_time = NOW() WHERE id = ?";
        return jdbcTemplate.update(sql, newContent, articleId);

    }

    @Override
    public int updateTitle(Integer id, String newTitle) {
        return 0;
    }

    @Override
    public int deleteById(Integer id) {
        return 0;
    }

    @Override
    public List<Article> selectByUserId(Integer userId) {
        String sql = "SELECT * FROM tb_article ta WHERE ta.user_id = ? " ;
        return jdbcTemplate.query(sql,articleRowMapper, userId);
    }

    @Override
    public List<Article> selectByKeyWord(String keyword) {
        String sql = "SELECT * FROM tb_article ta WHERE ta.title LIKE ? OR ta.content LIKE ?";
        String keyWordLike = "%" + keyword + "%";

        return jdbcTemplate.query(sql, articleRowMapper, keyWordLike, keyWordLike);

    }

    @Override
    public List<Article> selectByTimePeriod(LocalDateTime startTime, LocalDateTime endTime) {
        return null;
    }

    @Override
    public List<Article> selectByTag(Integer tagId) {
        return null;
    }

    @Override
    public List<Article> selectByCategory(Integer categoryId) {
        return null;
    }

    /**
     *  RowMapper接口的实现类，用于将查询结果映射为Article对象
     */

    private static class ArticleRowMapper implements RowMapper<Article> {
        @Override
        public Article mapRow(ResultSet rs, int rowNum) throws SQLException {
            /**
             * 结果封装逻辑
             */
            Article article = new Article();
            article.setId(rs.getInt("id"));
            article.setUserId(rs.getInt("user_id"));
            article.setTitle(rs.getString("title"));
            article.setContent(rs.getString("content"));
            article.setCategoryId(rs.getInt("category_id"));
            article.setCreateTime(rs.getTimestamp("create_time").toLocalDateTime());
            article.setUpdateTime(rs.getTimestamp("update_time").toLocalDateTime());
            return article;
        }
    }

    @Override
    public Article selectById(Integer id) {
        return null;
    }
}
